package com.itheima.d3_annotation3;

import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class AnnotationDemo {

    public static void main(String[] args) throws Exception, IllegalAccessException {
        //创建对象
        AnnotationDemo ad = new AnnotationDemo();
    //1.获取类的对象
        Class c = AnnotationDemo.class;
        //2.获取所有方法对象
        Method[] methods = c.getDeclaredMethods();
        //3.遍历所有方法
        for (Method method : methods) {
            //4.判断方法上是否有MyTest注解
            //有注解
            if(method.isAnnotationPresent(MyTest.class)){
                //5.方法上有MyTest注解 那么拿到注解的对象  myTest
                MyTest myTest = method.getDeclaredAnnotation(MyTest.class);
                //把注解的值 赋给变量 count
                int count = myTest.count();
                //6.有就执行方法 循环count次
                for (int i = 0; i < count; i++) {
                    method.invoke(ad);
                }
            }
        }
    }


    @MyTest(count = 3)
    public void test1(){
        System.out.println("test1方法执行了...");
    }
    public void test2(){
        System.out.println("test2方法执行了...");
    }
    @MyTest
    public void test3(){
        System.out.println("test3方法执行了...");
    }
    @MyTest(count = 5)
    public void test4(){
        System.out.println("test4方法执行了...");
    }

}
